$(function () {
    var params = Params();
    loginTest();
    isUser();
    linkAge();
    carousel();
    initDetails(); /*根据ID进行页面修改*/
    num_change();  /*订单数量改变*/
    shopCarData();      /*点击购物车存数据*/
    buyData();      /*点击购买存数据*/
    bigGlass();  /*放大镜*/
    shopClassify();  /*店铺分类*/
    goodsSwitch(); /*产品推荐与热门关注切换*/
    detailsComments();  /*商品详情和商品评价切换*/
    replyAssess();  /*商品评价回复*/



    /*判断用户是否存在*/
    function isUser(){
        var userName=LsyCookie.get('loginUser');
        if(userName){
            $('#buyNow').prop("href","confirm_order.html");
        }
    }
    /*三级联动*/
    function linkAge(){
        var prov = $('#prov');
        var city = $('#city');
        var county = $('#county');
        var provStr='<option value="0">--请选择省--</option>';
        var citys=null,countys=null;
        $('#address').click(function(){
            $('#addressDiv').css("display","block");
        });
        for(var i=0;i<PRO.length;i++){
            provStr+='<option value="'+PRO[i].ProID+'">'+PRO[i].name+'</option>'
        }
        prov.html(provStr);
        prov.change(function(){
            var val=Number($(this).val());
            citys=getCitysByProId(val);
            var cityStr='<option value="0">--请选择市--</option>';
            for(var i=0;i<citys.length;i++){
                cityStr+='<option value="'+citys[i].CityID+'">'+citys[i].name+'</option>'
            }
            city.html(cityStr);
            county.html('<option value="0">--请选择县--</option>')
        });
        city.change(function(){
            var val=Number($(this).val());
            countys=getCountyByCityId(val);
            var countyStr='<option value="0">--请选择县--</option>';
            for(var i=0;i<countys.length;i++){
                countyStr+='<option>'+countys[i].DisName+'</option>'
            }
            county.html(countyStr);
        });
        county.change(function(){
            var addressText='';
            if(city.val()==1){
                num=city.val()-1;
            }
            else if(city.val()==2){
                num=city.val()-1;
            }
            else{
                num=city.val()-2;
            }
            addressText+=PRO[prov.val()-1].name+'&nbsp'+CITYS[num].name+'&nbsp'+$(this).val()+'<img src="img/comments_4.png" alt="comments_4.png">';
            $('#address').html(addressText);
            $('#addressDiv').css("display","none");
        });
    }
    /*商品评价回复*/
    function replyAssess(){
        var details_li=$('#details li');
        var praise_b=$('.praise b');
        var reply=$('.reply');
        var reply_sub=$('.reply input');
        var boole=false;
        praise_b.click(function(){
            var index=$(this).parents('li').index();
            if(!boole){
                details_li.eq(index).find('.reply').css("display","block");
                boole=true;
            }
            else {
                details_li.eq(index).find('.reply').css("display","none");
                boole=false;
            }
        });
        reply_sub.click(function(e){
            reply.css("display","none");
            boole=false;
            e.preventDefault();
        });
    }
    /*产品展示图片切换和放大镜*/
    function bigGlass(){
        var goods_pic=$('#goods_list .pic');
        var goods_small=$('#goods_list .pic .small');
        var goods_big=$('#goods_list .big');
        var goods_li=$('#goods_list ul li');
        var x= 0,y=0;
        goods_pic.mousemove(function(e){
            x=e.clientX-$(this).get(0).getBoundingClientRect().left;
            y=e.clientY-$(this).get(0).getBoundingClientRect().top;
            if(x<=96){
                x=96;
            }
            if(y<88){
                y=88;
            }
            if(x>288){
                x=288;
            }
            if(y>264){
                y=264;
            }
            goods_small.css({"left":x-96,"top":y-88});
            goods_big.find('img').css({"top":-2*y+176,"left":-2*x+192});
        });
        goods_li.mouseenter(function(){
            $(this).addClass('on').siblings().removeClass('on');
            var img_src=$(this).find('img').attr("src");
            goods_pic.find('img').attr("src",img_src);
            goods_big.find('img').attr("src",img_src);
        });
        goods_pic.hover(function(){
            goods_small.css("display","block");
            goods_big.css("display","block");
        },function(){
            goods_small.css("display","none");
            goods_big.css("display","none");
        });

    }
    /*店铺分类切换效果*/
    function shopClassify(){
        var classify_label=$('#classify label');
        var bool=true;
        classify_label.click(function(){
            if(bool){
                $(this).find('i').css("background","url('img/comments_16.png') no-repeat");
                bool=false;
            }
            else{
                $(this).find('i').css("background","url('img/comments_15.png') no-repeat");
                bool=true;
            }
        });
    }
    /*产品推荐与热门关注切换*/
    function goodsSwitch(){
        var products_hot=$('#products,#hot');
        var products=$('#products');
        var hot=$('#hot');
        var goods=$('#goods');
        products_hot.click(function(){
            $(this).addClass('on').siblings().removeClass('on');
        });
        products.click(function(){
            goods.stop().animate({'left':0});
        });
        hot.click(function(){
            goods.stop().animate({'left':-208});
        });
    }
    /*订单数量改变*/
    function num_change(){
        var num_reduce=$('#num i');
        var num_plus=$('#num span');
        var num=0;
        var numInp=$('#num input');
        numInp.keyup(function(){
            var textInp=numInp.val();
            textInp=integer(textInp);
            numInp.val(textInp);
        });
        numInp.blur(function(){
            var textInp=numInp.val();
            if(!textInp){
                textInp=1;
            }
            numInp.val(textInp);
        });
        num_reduce.click(function(){
            num=$('#num input').val();
            num--;
            if(num==0){
                num=1;
            }
            $('#num input').val(num);
        });
        num_plus.click(function(){
            num=$('#num input').val();
            num++;
            $('#num input').val(num);
        })
    }
    /*根据ID进行页面修改*/
    function initDetails() {
        var id=params.id;
        id=id?id:1;
        var details = LsyStorage.getItem('shopList_' + id);
        var goods_img = $('#goods_list .pic img');
        var goods_img_1 = $('#goods_list ul li:first-child img');
        var goods_img_2 = $('#goods_list .big img');
        var title_h1 = $('#title h1');
        var consult_price = $('#consult span');
        var present_price = $('#present_price i');
        var top_a = $('#top a:last-child');
        var introduceTitle=$('#introduce li:first-child b');
        goods_img.attr("src", details.imgSrc);
        goods_img_1.attr("src", details.imgSrc);
        goods_img_2.attr("src", details.imgSrc);
        title_h1.html(details.title);
        introduceTitle.html(details.title);
        consult_price.text(details.old_price);
        present_price.text(details.price);
        top_a.attr("href", 'comments.html?id=' + details.id)
    }
    /*点击购物车存数据*/
    function shopCarData(){
        var shopCar=$('#shopCar');
        shopCar.click(function(){
            var num=$('#num input').val();
            num=Number(num);
            if(num>0){
                alert('已存入购物车');
                var id=params.id;
                id=id?id:1;
                var shopCarDatas=LsyStorage.getItem('shopCar_'+id);
                var shopListDatas=LsyStorage.getItem('shopList_'+id);
                if(shopCarDatas==null){
                    shopListDatas.checked=true;
                    shopListDatas.num=num;
                    LsyStorage.setItem('shopCar_'+id,shopListDatas);
                }else {
                    shopCarDatas.num=Number(shopCarDatas.num)+num;
                    LsyStorage.setItem('shopCar_'+id,shopCarDatas);
                }
            }
        })
    }
    /*点击购买存数据*/
    function buyData(){
        var buyNow=$('#buyNow');
        buyNow.click(function(){
            var id=params.id;
            id=id?id:1;
            LsyStorage.removeArr('buyList');
            var num=$('#num input').val();
            num=Number(num);
            var shopListDatas=LsyStorage.getItem('shopList_'+id);
            shopListDatas.num=num;
            LsyStorage.setItem('buyList_'+id,shopListDatas);
        })
    }
    /*商品详情和商品评价切换*/
    function detailsComments(){
        var topA=$('#top span');
        var tapDiv=$('#tap>div')
        topA.click(function(){
            $(this).addClass('on').siblings().removeClass('on');
            index=$(this).index();
            tapDiv.eq(index).css("display","block").siblings().css("display","none")
        })
    }
    function carousel(){
        var mySwiper = new Swiper ('.swiper-container', {
//        direction: 'vertical',
            loop: true,
            paginationClickable :true,
            // 如果需要分页器
            pagination: '.swiper-pagination',

            // 如果需要前进后退按钮
            nextButton: '.swiper-button-next',
            prevButton: '.swiper-button-prev',

        })
    }
});